package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;
import com.stark.ve.core.SpeedMode;

import flc.ast.BaseEditVideoFragment;
import flc.ast.R;
import flc.ast.activity.VideoEditActivity;
import flc.ast.databinding.FragmentVideoSpeedBinding;
import flc.ast.utils.Content;
import flc.ast.view.BubbleSeekBar;
import stark.common.basic.utils.ProgressConvertUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link VideoSpeedFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class VideoSpeedFragment extends BaseEditVideoFragment<FragmentVideoSpeedBinding> {

    private static final int MAX_PROGRESS = 100;
    public static final int INDEX = Content.INDEX_SPEED;
    private float mCurSpeed = 1.0f;


    public static VideoSpeedFragment newInstance() {
        VideoSpeedFragment fragment = new VideoSpeedFragment();
        return fragment;
    }


    @Override
    public void onShow() {
        mVideoEditActivity.mode = VideoEditActivity.MODE_SPEED;
    }

    @Override
    public void backToMain() {
//        mVideoEditActivity.mVideoView.setVideoPath(mVideoEditActivity.getMainPath());
        mVideoEditActivity.mVideoView.setVideoPath(mVideoEditActivity.getMainPath());
        mVideoEditActivity.mVideoView.seekTo(1);
        mVideoEditActivity.mVideoView.start();
        mVideoEditActivity.mImageView.setImageResource(R.drawable.azant);
        mVideoEditActivity.mode = VideoEditActivity.MODE_NONE;
        mVideoEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_video_speed;
    }

    @Override
    protected void initView() {
        mDataBinding.sbarSpeed.setProgress(21);
        float minSpeed = EditorHolder.editor().supportMinSpeed();
        float maxSpeed = EditorHolder.editor().supportMaxSpeed();
        mDataBinding.sbarSpeed.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {
                if (fromUser) {
                    mCurSpeed = ProgressConvertUtil.progress2value(progress, minSpeed, maxSpeed, MAX_PROGRESS);
                    mDataBinding.sbarSpeed.mBubbleText = mCurSpeed;
                }
            }

            @Override
            public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {

            }

            @Override
            public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {

            }
        });
    }

    @Override
    protected void initData() {
        onShow();
    }

    public void applySpeedVideo() {
        if (mVideoEditActivity.mVideoView.isPlaying()) {
            mVideoEditActivity.mVideoView.pause();
            mVideoEditActivity.mImageView.setImageResource(R.drawable.abof);
        }
        showDialog(getString(R.string.video_speed_loading) + 0 + "%");
        EditorHolder.editor().videoChangeSpeed(mVideoEditActivity.getMainPath(), mCurSpeed, SpeedMode.ALL, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                ToastUtils.showShort(R.string.video_speed_success);
                mVideoEditActivity.changeMainPath(outFilePath, true);
                backToMain();
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(R.string.video_speed_fail);
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.video_speed_loading) + progress + "%");
            }
        });
    }

}